package model

import (
	"src/config"
	"src/util/time"
)

func GetBusByroute(routeid int, busid string) []BusMessage {
	var data []BusMessage
	flag, flagoppo := false, false
	//d1 := &BusMessage{}
	//RedisDBCntlr := NewRedisDBCntlr()
	timenow := time.GetSecondTimestamp()
	for k, v := range config.Rou.BusRoute {
		if v == routeid && (busid == "" || busid == k) {
			id := config.Busmessages.BusToId[k]
			//_Id, err := RedisDBCntlr.GET(k)
			if BusesFlag[id] != true {
				//if err != nil {
				continue
			} else if timenow <= BusesEXPTime[id] {
				//} else {
				//d, err := RedisDBCntlr.GET(_Id)
				//if err != nil {
				//} else {
				//	_ = json.Unmarshal([]byte(d), d1)
				//	if d1.Lat != 0 {
				//		data = append(data, *d1)
				//	}
				//}
				if BusesMessage[id].Direction == 0 || BusesMessage[id].Direction == 2 || BusesMessage[id].Direction == 5 {
					flag = true
				} else {
					flagoppo = true
				}
				data = append(data, BusesMessage[id])
			}
			//data = append(data, BusesMessage[id])
		}
	}
	//RedisDBCntlr.Close()
	if !flag {
		tem := Station{}
		a := BusMessage{
			Lat:       0,
			Lon:       9999,
			Direction: 0,
			Distance:  9999,
		}
		for _, j := range config.Rou.Routes[routeid].Points {
			tem.PreStaDistance = j.PreStaDistance
			tem.LasStaDistance = j.LasStaDistance
			tem.Flag = j.Flag
			tem.Name = j.Name
			a.Stations = append(a.Stations, tem)
		}
		data = append(data, a)
	}
	if !flagoppo {
		tem := Station{}
		b := BusMessage{
			Lat:       0,
			Lon:       9999,
			Direction: 1,
			Distance:  9999,
		}
		for _, j := range config.Rou.Routes[routeid].OppositePoints {
			tem.PreStaDistance = j.PreStaDistance
			tem.LasStaDistance = j.LasStaDistance
			tem.Flag = j.Flag
			tem.Name = j.Name
			b.Stations = append(b.Stations, tem)
		}
		data = append(data, b)
	}
	return data
}
